/**
 * Copyright © 2011 Software in Life Inc. All rights reserved.
 */
package com.softwareinlife.snaf.user.dao;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import org.springframework.stereotype.Repository;

import com.softwareinlife.snaf.user.model.UserModel;

/**
 * <b>Data Access Object for User</b>
 * 
 * <p>
 * For a complete reference see <a href=
 * "http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/" > Hibernate
 * Reference Documentation</a>
 * </p>
 * 
 * @author Jang, Sun-Jin(jangsunjin@softwareinlife.com)
 */
@Repository
public class UserDao {

	/**
	 * Logger
	 */
	protected static Logger logger = Logger.getLogger("dao");

	/**
	 * PMF
	 */
	private static final PersistenceManagerFactory pmf = JDOHelper
			.getPersistenceManagerFactory("transactions-optional");

	/**
	 * Create
	 * 
	 * @param model
	 */
	public void create(UserModel model) {

		// Retrieve session from Hibernate
		PersistenceManager pm = pmf.getPersistenceManager();

		try {
			// Save or Update model
			pm.makePersistent(model);
		} finally {
			pm.close();
		}
	}

	/**
	 * Select All
	 * 
	 * @return a list of models
	 */

	public List<UserModel> getModels() {
		logger.info(this.getClass() + " Retrieving all");

		// return values
		List<UserModel> models = null;

		// Retrieve session from Hibernate
		PersistenceManager pm = pmf.getPersistenceManager();

		try {
			pm.retrieveAll(models);

			// Create a Hibernate query (HQL)
			// Query query = session.createQuery("FROM UserModel");

			// Retrieve all
			models = (List<UserModel>) pm.getObjectById(UserModel.class);
		} finally {
			pm.close();
		}

		// return new UsersModel(models);
		return models;
	}

	/**
	 * Read
	 * 
	 * @param email
	 * @return Model
	 */
	public UserModel read(String email) {
		logger.info(this.getClass() + "Select userEmail is " + email);

		// Return values
		UserModel model = null;

		// Retrieve session from Hibernate
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			// Get Results
			model = pm.getObjectById(UserModel.class, email);
		} finally {
			pm.close();
		}

		return model;

	}

	/**
	 * Update
	 * 
	 * @param model
	 */
	public void update(UserModel model) {

		// Retrieve session from Hibernate
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			// Save or Update model
			pm.makePersistent(model);
		} finally {
			pm.close();
		}

	}

	/**
	 * Delete
	 * 
	 * @param id
	 *            the id of the existing model
	 */

	public void delete(UserModel model) {
		logger.info(this.getClass() + "Deleting existing " + model);

		// Retrieve session from Hibernate
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			// Delete model
			pm.deletePersistent(model);
		} finally {
			pm.close();
		}

	}

	/**
	 * Delete
	 * 
	 * @param id
	 *            the id of the existing model
	 */

	public void delete(String email) {
		logger.info(this.getClass() + "Deleting existing " + email);

		// Retrieve session from Hibernate
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			// Retrieve model
			UserModel model = (UserModel) pm.getObjectById(UserModel.class,
					email);

			// Delete model
			pm.deletePersistent(model);
		} finally {
			pm.close();
		}

	}
}
